# import os
# from datetime import date

# import django
# from django.db.models import F, Q, Sum, Min, Max, Avg

# os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'demo.settings')
# django.setup()

# from app_models.models import BookInfo, HeroInfo

# books = BookInfo.objects.filter(btitle='天龙八部')
# print(books.query)
# print(books)

# book = BookInfo(
#     btitle='西游记',
#     bpub_date=date(1988, 1, 1),
#     bread=10,
#     bcomment=10
# )
# book.save()
# print(book)


# books = BookInfo.objects.exclude(id=1)
# # print(books.query)
# print(books)
# books = BookInfo.objects.exclude(id=1)
# print(books)
# for book in books:
#     print(book)
#
# for book in books:
#     print(book)

# 测试一下 自定义的 manager
# book = BookInfo.objects.hot()
# print(book)

# 一，添加数据
# - 1.创建名为西游记的图书，添加到数据库中
# book = BookInfo.objects.get(btitle='西游记')
# - 2.创建孙悟空的英雄，关联西游记，添加到书库中
# hero = HeroInfo(hname='孙悟空', hbook=book)
# hero.save()
# hero = HeroInfo(hname='孙悟空2', hbook_id=book.id)
#
# 二，查询数据
#
# - ===一、基本查询===
# - 1.查询编号为1的图书【两种方式】
# BookInfo.objects.filter(id=1)  # 得到的是查询集
# BookInfo.objects.get(id=1)  # 有且只有一个id=1的数据,才能正确
# - 2.查询所有图书的数量
# BookInfo.objects.filter().count()
# - 3.查询书名包含雪山的图书【开头，结尾】
# BookInfo.objects.filter(btitle__startswith='雪山')
# BookInfo.objects.filter(btitle__endswith='雪山')
# - 4.查询书籍名不为空的书籍
# book = BookInfo.objects.get(id=6)
# book.btitle = None雪山
# book.save()
# book = BookInfo.objects.filter(img__isnull=False)
# print(book)
# - 5.查询书籍编号为1，3，5的图书
# books = BookInfo.objects.filter(id__in=[1, 3, 5])
# books = BookInfo.objects.filter(id__range=[1, 3])
# print(books)
# - 6.查询编号大于3的图书
# books = BookInfo.objects.filter(id__gt=3)
# print(books)
# - 7.查询编号大于等于3的图书
# books = BookInfo.objects.filter(id__gte=3)
# print(books)
# - 8.查询编号不等于3的图书
# books = BookInfo.objects.exclude(id=3)
# print(books)
# - 9.查询1980年发表的图书
# books = BookInfo.objects.filter(bpub_date__year='1980')
# print(books)
# - 10.查询1980年1月1号之后发表的图书
# books = BookInfo.objects.filter(bpub_date__gt='1990-01-01')
# print(books)
#
#
# - ===三、关联过滤查询===
#  根据关联的模型的属性过滤

# - 19.查询书籍编号为1，中的所有英雄
# heros = HeroInfo.objects.filter(hbook_id=1)
# print(heros)
# book = BookInfo.objects.get(id=1)
# heros = HeroInfo.objects.filter(hbook=book)
# print(heros)
# - 20.查询书籍名称为雪山飞狐的，所有的英雄
# heros = HeroInfo.objects.filter(hbook__btitle='雪山飞狐')
# print(heros)

# - 21.查询英雄编号为1，所属的图书
# book = HeroInfo.objects.get(id=1).hbook
# print(book.btitle)
# - 22.查询英雄中有乔峰的书籍
# books = BookInfo.objects.filter(heroinfo__hname='乔峰')
# print(books)
# - 23.查询英雄名字中包含‘郭’的书籍
# books = BookInfo.objects.filter(heroinfo__hname__contains='不').distinct()
# print(books)
# - 24.查询书籍阅读量大于30的书籍中的所有英雄
# heros = HeroInfo.objects.filter(hbook__bread__gt=30)
# print(heros)

# 取1号图书中的所有英雄
# book = BookInfo.objects.get(id=1)
# print(book.heros.all())



#
# - ===二、FQ查询===
# - 11.查询阅读量大于等于评论量的图书
# books = BookInfo.objects.all()
# book_list = []
# for book in books:
#     if book.bread>=book.bcomment:
#         book_list.append(book)
# BookInfo.objects.filter(bread__gte=F('bcomment'))
# - 12.查询阅读量大于等于2倍评论量的图书
# BookInfo.objects.filter(bread__gte=F('bcomment')*2)
# - 13.查询阅读量大于20，并且编号小于3的图书
# BookInfo.objects.filter(bread__gt=20, id__lt=3)
# BookInfo.objects.filter(bread__gt=20).filter(id__lt=3)
# - 14.查询阅读量大于20，或编号小于3的图书,  &  |  ~
# books = BookInfo.objects.filter(Q(bread__gt=20) | Q(id__lt=3))
# print(books)
# - 15.查询编号不等于3的图书，使用Q对象实现
# books = BookInfo.objects.filter(~Q(id=3))
# print(books)

# - 16.查询所有阅读量的总和
# - 17.查询最大的评论量
# bread_sum = BookInfo.objects.all().aggregate(Sum('bread'))
# print(bread_sum.get('bread__sum'))
# bread_avg = BookInfo.objects.all().aggregate(Avg('bread'))
# print(bread_avg)
# bread_min = BookInfo.objects.all().aggregate(Min('bread'))
# print(bread_min)
# bread_max = BookInfo.objects.all().aggregate(Max('bread'))
# print(bread_max)

# - 18.查询所有图书，按照阅读量排序，升序【降序】
# print(BookInfo.objects.all().order_by('bread')[:3])
# print(BookInfo.objects.all().order_by('-bread'))

# books = BookInfo.objects.filter(Q(id__gt=3) | Q(bread__gte=20)).order_by('bcomment')[:3]
# print(books.query)


# 三，修改数据
# - 1.修改书籍名为雪山飞狐的书籍，改为雪山飞猫
# - 2.修改英雄名为孙悟空的任务，改名为孙大圣
#
# 四，删除数据
# - 1.删除编号为 10 的英雄
# - 2.删除英雄名为 郭靖 的英雄


dict_list = [
    {
        "name":"mff",
        "page":18,
        "number":1
    },
    {
        "name":"mff1",
        "page":20,
        "number":1
    },
    {
        "name":"mff2",
        "page":23,
        "number":0
    }
]

number = [1,0]


for num in number:

    for dicts in dict_list:

        # print(dicts['number'])

        if num == dicts['number']:

            print(num,dicts['name'])

        # else:

        #     print('no')
        
# 1 mff,mff1    0 mff
